﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Web.UI.WebControls;


/// <summary>
/// Summary description for Common
/// </summary>
public class Common
{
    public Common()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public static Int64 createID(){
        TimeSpan timeSpan = new TimeSpan(DateTime.UtcNow.Ticks);
        Int64 id = (Int64)timeSpan.TotalMilliseconds;
        return id;
    }

    /* Convert ngày hiện tại sang Seconds */
    public static double ToSeconds(DateTime datetime)
    {
        double seconds = 0;
        DateTime systemDate = new DateTime(2011, 1, 1, 0, 0, 0);
        TimeSpan subDate = datetime - systemDate;

        seconds = Math.Round(subDate.TotalSeconds);
        return seconds;
    }

    public static String confirmDelete(String message)
    {
        String msg = message == null || message.Equals("")  ? "Bạn thật sự muốn xóa dữ liệu này?": message ;
        return "<script>confirm('"+ msg+"')</script>";
    }

    public static void resizeImage(String path, Int32 width, Int32 height, FileUpload source)
    {
        try
        {
            System.Drawing.Image oImg = System.Drawing.Image.FromStream(source.PostedFile.InputStream);
            System.Drawing.Image oThumbNail = new Bitmap(width, height, oImg.PixelFormat);
            Graphics oGraphic = Graphics.FromImage(oThumbNail);
            oGraphic.CompositingQuality = CompositingQuality.HighQuality;
            oGraphic.SmoothingMode = SmoothingMode.HighQuality;
            oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            Rectangle oRectangle = new Rectangle(0, 0, width, height);
            oGraphic.DrawImage(oImg, oRectangle);
            oThumbNail.Save(path, ImageFormat.Jpeg);
            oImg.Dispose();
        }
        catch (Exception ex) { }
    }

    public static void resizeImageLogo(String path, Int32 width, Int32 height, FileUpload source)
    {
        try
        {
            System.Drawing.Image oImg = System.Drawing.Image.FromStream(source.PostedFile.InputStream);
            System.Drawing.Image oThumbNail = new Bitmap(width, height, oImg.PixelFormat);
            Graphics oGraphic = Graphics.FromImage(oThumbNail);
            oGraphic.CompositingQuality = CompositingQuality.HighQuality;
            oGraphic.SmoothingMode = SmoothingMode.HighQuality;
            oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            Rectangle oRectangle = new Rectangle(0, 0, width, height);
            oGraphic.DrawImage(oImg, oRectangle);
            oThumbNail.Save(path, ImageFormat.Png);
            oImg.Dispose();
        }
        catch (Exception ex) { }
    }

    
}
